package com.example.ks.servlet;

import com.example.ks.dao.impl.OrderDaoImpl;
import com.example.ks.entity.Order;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/submit-appointment")
public class SubmitAppointmentServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置编码
        request.setCharacterEncoding("UTF-8");

        // 打印接收到的参数（用于调试）
        System.out.println("接收到的参数:");
        System.out.println("adminId: " + request.getParameter("adminId"));
        System.out.println("userId: " + request.getParameter("userId"));
        System.out.println("startTime: " + request.getParameter("startTime"));
        System.out.println("serviceItem: " + request.getParameter("serviceItem"));
        System.out.println("petName: " + request.getParameter("petName"));

        try {
            int adminId = Integer.parseInt(request.getParameter("adminId"));
            int userId = Integer.parseInt(request.getParameter("userId"));
            String startTimeStr = request.getParameter("startTime");
            String serviceItem = request.getParameter("serviceItem");
            String petName = request.getParameter("petName");

            // 检查 startTimeStr 是否为 null 或空字符串
            if (startTimeStr == null || startTimeStr.isEmpty()) {
                request.setAttribute("error", "请提供有效的开始时间");
                request.getRequestDispatcher("user-index.jsp").forward(request, response);
                return;
            }

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
            Date startTime = sdf.parse(startTimeStr);

            Order order = new Order();
            order.setAdminId(adminId);
            order.setUserId(userId);
            order.setStartTime(startTime);
            order.setServiceItem(serviceItem);
            order.setPetName(petName);
            order.setStatus(0); // 默认状态：服务未开始

            OrderDaoImpl orderDao = new OrderDaoImpl();
            int orderId = orderDao.createOrder(order);

            if (orderId != -1) {
                // 预约成功，重定向到成功页面
                response.sendRedirect("appointment-success.jsp");
            } else {
                // 预约失败
                request.setAttribute("error", "预约失败，请稍后再试");
                request.getRequestDispatcher("user-index.jsp").forward(request, response);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            request.setAttribute("error", "参数格式错误: " + e.getMessage());
            request.getRequestDispatcher("user-index.jsp").forward(request, response);
        } catch (ParseException e) {
            e.printStackTrace();
            request.setAttribute("error", "日期格式错误，请使用正确的日期时间格式");
            request.getRequestDispatcher("user-index.jsp").forward(request, response);
        } catch (SQLException e) {
            e.printStackTrace();
            request.setAttribute("error", "数据库错误: " + e.getMessage());
            request.getRequestDispatcher("user-index.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("error", "未知错误: " + e.getMessage());
            request.getRequestDispatcher("user-index.jsp").forward(request, response);
        }
    }
}